<template>
  <el-dialog v-model="store.employeeLoginFormVisible" title="员工登录"  width="30%" center>
    <el-form
        :rules="rules"
        label-width="70px"
        ref="dom"
        :model="employeeMain"
        status-icon
        style="width: 366px;margin: auto;"
    >
      <el-form-item label="ID :" prop="id">
        <el-input v-model="employeeMain.id" type="text"/>
      </el-form-item>

      <el-form-item label="密码 :" prop="password">
        <el-input v-model="employeeMain.password" type="password"/>
      </el-form-item>

    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="store.addStoreFormVisible = false">取消</el-button>
        <el-button :plain="true" type="primary" @click="submit(dom)"
        >登录
        </el-button>
<!--        <el-button type="primary"  link @click="()=>{router.push('/admin/register')}">注册</el-button>-->
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import {inject, reactive, ref,} from "vue";
import {store} from "@/store";
import {Location} from "@element-plus/icons-vue";
import request from "@/utils/request";
import {ElMessage} from "element-plus";
import {message} from "ant-design-vue";

const dom = ref()
const employeeMain=reactive({})
const iinject=inject('reload')

const rules = reactive({
  id: [{required: true, message: '请输入账号' }],
  password: [{required: true, message: '请输入密码' }],
})

const submit = (dom) => {
  dom.validate((isTrue) => {
    if (isTrue) {
        if (store.getLocalEmployeeMain()!=null&&employeeMain.id===store.getLocalEmployeeMain().id){
            ElMessage({
                message: "与当前账号相同",
                type:'warning',
            })
            return
        }
      employeeMain.storeId=store.getLocalCurrentStoreMain().storeId
      request.post("/store/loginEmployeeMain", employeeMain).then(response => {
        if (response.code === 200) {
          store.setLocalStorage("employeeMain",response.data)
          store.setLocalStorage("rsms-token", response.data.token)
          store.employeeLoginFormVisible=false
          iinject()
        } else {
          console.log(response)
          ElMessage({
            message: response.message,
            type:'error',
          })
          return Promise.reject("登陆失败")
        }
      })
    } else {
      console.log("格式有误")
    }
  })
}


</script>

<style scoped>

</style>